package com.kili.file.service.impl;

import com.kili.file.controller.request.FileUploadRequest;
import com.kili.file.exception.FileUploadException;
import com.kili.file.facade.picture.PictureResult;
import com.kili.file.facade.picture.PictureUploadServiceFacade;
import com.kili.file.service.ImageUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.Objects;

/**
 * @author Gordon
 * @version 2019/5/17
 */
@Service
@Slf4j
public class ImageUploadServiceImpl implements ImageUploadService {

    @Autowired
    private PictureUploadServiceFacade pictureUploadServiceFacade;

    @Override
    public String imageUploadHandler(FileUploadRequest request) {
        PictureResult result = pictureUploadRequest(request);
        return result.getData().toString();
    }

    private PictureResult pictureUploadRequest(FileUploadRequest request) {
        PictureResult result = pictureUploadServiceFacade.pictureUpload(request.swap());
        if (Objects.isNull(result) || !result.isSuccess() || Objects.isNull(result.getData())) {
            throw new FileUploadException("Picture upload failed");
        } else {
            return result;
        }
    }
}
